<script setup>
import http from "@/utils/http.js";
import {onMounted, ref} from "vue";
import {useRoute} from "vue-router";
import {userStore} from "@/stores/userStore.js";
import {ElMessage} from "element-plus";

import avatar from '@/assets/images/Aou.jpg'

const route = useRoute()
const uStore = userStore()

const goodsData = ref([])
const getGoods = async () => {
  const res = await http.get('/goods/' + route.params.id)
  goodsData.value = res.data
}

const commentList = ref([])
const getComments = async () => {
  const res = await http.get('/comments/' + route.params.id)
  console.log(res.data)
  commentList.value = res.data
}

const addGoods = ref({
  itemUser: uStore.userInfo.data.userId,
  itemGoods: ''
})

const add = async () => {
  addGoods.value.itemGoods = goodsData.value.goodsId
  const res = await http.post('/items/add', addGoods.value)

  if (res.code === 666) {
    ElMessage({type: 'success', message: '添加成功'})
  }
}

const commentData = ref({
  commentUser: uStore.userInfo.data.userId,
  commentGoods: '',
  content: ''
})

const commit = async () => {
  commentData.value.commentGoods = goodsData.value.goodsId
  const res = await http.post('/comments/add', commentData.value)

  if (res.code === 666) {
    ElMessage({type: 'success', message: '评论成功'})

    commentData.value = {
      commentUser: uStore.userInfo.data.userId,
      commentGoods: '',
      content: ''
    }

    await getComments()
  }
}

onMounted(() => {
  window.scroll({top: 0})
  getGoods()
  getComments()
})
</script>

<template>
  <div class="goods-page">
    <div class="container">
      <!-- 商品信息 -->
      <div class="product-container">
        <div class="product-card">
          <div class="product-media">
            <img :src="goodsData.goodsPhoto" alt="商品图片" class="product-image" />
          </div>
          <div class="product-details">
            <h1 class="product-title">{{ goodsData.goodsName }}</h1>
            <p class="product-description">{{ goodsData.goodsDescription }}</p>
            
            <div class="price-section">
              <span class="product-price">¥{{ goodsData.goodsPrice }}</span>
            </div>
            
            <div class="delivery-info">
              <div class="info-item">
                <span class="info-label">发货地:</span>
                <span class="info-value">{{ goodsData.goodsAddress }}</span>
              </div>
              <div class="info-item">
                <span class="info-label">发布时间:</span>
                <span class="info-value">{{ goodsData.createTime }}</span>
              </div>
            </div>
            
            <div class="service-info">
              <h3 class="service-title">服务承诺</h3>
              <div class="service-tags">
                <span class="service-tag">无忧退货</span>
                <span class="service-tag">快速退款</span>
                <span class="service-tag">免费包邮</span>
              </div>
            </div>
            
            <el-button 
              type="primary" 
              size="large" 
              class="add-to-cart-btn"
              @click="add()"
            >
              <i class="el-icon-shopping-cart"></i> 加入购物车
            </el-button>
          </div>
        </div>
        
        <!-- 评论区域 -->
        <div class="comments-section">
          <div class="comments-header">
            <h2 class="section-title">商品评价</h2>
            <span class="comments-count">({{ commentList.length }}条)</span>
          </div>
          
          <div class="comment-editor">
            <el-avatar :src="uStore.userInfo.data.userAvatar || avatar" size="small" />
            <el-input
              v-model="commentData.content"
              type="textarea"
              :rows="3"
              placeholder="写下你的真实感受，帮助更多人了解这个商品..."
              class="comment-input"
            />
            <el-button 
              type="primary" 
              class="submit-comment-btn"
              @click="commit()"
            >
              发表评论
            </el-button>
          </div>
          
          <div class="comments-list">
            <div v-for="comment in commentList" :key="comment.commentId" class="comment-item">
              <div class="comment-header">
                <el-avatar :src="comment.userAvatar || avatar" size="medium" />
                <div class="comment-user">
                  <span class="username">{{ comment.userNickname }}</span>
                  <span class="comment-time">{{ comment.createTime }}</span>
                </div>
              </div>
              <div class="comment-content">
                {{ comment.content }}
              </div>
            </div>
            
            <div v-if="commentList.length === 0" class="no-comments">
              <i class="el-icon-chat-line-round"></i>
              <p>暂无评价，快来发表第一条评论吧</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.goods-page {
  background-color: #f5f7fa;
  min-height: 100vh;
  padding: 40px 0;
  font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
  
  .container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 20px;
  }
}

.product-container {
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  margin-bottom: 30px;
}

.product-card {
  display: flex;
  padding: 30px;
  
  @media (max-width: 768px) {
    flex-direction: column;
  }
}

.product-media {
  flex: 0 0 45%;
  padding: 20px;
  
  .product-image {
    width: 100%;
    height: auto;
    max-height: 500px;
    object-fit: contain;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s ease;
    
    &:hover {
      transform: scale(1.02);
    }
  }
}

.product-details {
  flex: 1;
  padding: 0 30px;
  
  .product-title {
    font-size: 24px;
    font-weight: 600;
    color: #333;
    margin-bottom: 15px;
    line-height: 1.4;
  }
  
  .product-description {
    font-size: 15px;
    color: #666;
    line-height: 1.6;
    margin-bottom: 25px;
  }
}

.price-section {
  margin: 25px 0;
  
  .product-price {
    font-size: 28px;
    font-weight: 700;
    color: #ff6b6b;
  }
}

.delivery-info {
  background: #f8f9fa;
  padding: 15px;
  border-radius: 8px;
  margin: 20px 0;
  
  .info-item {
    display: flex;
    margin-bottom: 8px;
    
    &:last-child {
      margin-bottom: 0;
    }
  }
  
  .info-label {
    color: #666;
    width: 80px;
  }
  
  .info-value {
    color: #333;
    font-weight: 500;
  }
}

.service-info {
  margin: 25px 0;
  
  .service-title {
    font-size: 16px;
    color: #666;
    margin-bottom: 10px;
  }
  
  .service-tags {
    display: flex;
    gap: 10px;
  }
  
  .service-tag {
    background: #f0f7ff;
    color: #3a86ff;
    padding: 5px 12px;
    border-radius: 15px;
    font-size: 13px;
  }
}

.add-to-cart-btn {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  background: linear-gradient(135deg, #8ba3a0, #6d8b89);
  border: none;
  border-radius: 8px;
  margin-top: 20px;
  transition: all 0.3s ease;
  
  &:hover {
    background: linear-gradient(135deg, #6d8b89, #5a7573);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(109, 139, 137, 0.3);
  }
}

.comments-section {
  padding: 30px;
  border-top: 1px solid #eee;
  
  .comments-header {
    display: flex;
    align-items: center;
    margin-bottom: 25px;
    
    .section-title {
      font-size: 20px;
      font-weight: 600;
      color: #333;
      margin-right: 10px;
    }
    
    .comments-count {
      color: #999;
      font-size: 14px;
    }
  }
}

.comment-editor {
  display: flex;
  flex-direction: column;
  gap: 15px;
  margin-bottom: 30px;
  background: #f8f9fa;
  padding: 20px;
  border-radius: 8px;
  
  .comment-input {
    :deep(.el-textarea__inner) {
      border-radius: 8px;
      border: 1px solid #e1e4e8;
      resize: none;
      transition: border-color 0.3s;
      
      &:focus {
        border-color: #8ba3a0;
        box-shadow: 0 0 0 2px rgba(139, 163, 160, 0.2);
      }
    }
  }
  
  .submit-comment-btn {
    align-self: flex-end;
    background: #8ba3a0;
    border: none;
    padding: 8px 20px;
    
    &:hover {
      background: #6d8b89;
    }
  }
}

.comments-list {
  .comment-item {
    padding: 20px 0;
    border-bottom: 1px solid #f0f0f0;
    
    &:last-child {
      border-bottom: none;
    }
  }
  
  .comment-header {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
  }
  
  .comment-user {
    margin-left: 12px;
    display: flex;
    flex-direction: column;
    
    .username {
      font-weight: 500;
      color: #333;
    }
    
    .comment-time {
      font-size: 12px;
      color: #999;
      margin-top: 2px;
    }
  }
  
  .comment-content {
    color: #333;
    line-height: 1.6;
    padding-left: 52px;
  }
}

.no-comments {
  text-align: center;
  padding: 40px 0;
  color: #999;
  
  i {
    font-size: 40px;
    margin-bottom: 15px;
    color: #ddd;
  }
  
  p {
    margin-top: 10px;
  }
}
</style>